#ifndef CALCULATOR_H
#define CALCULATOR_H

#include <QMainWindow>
#include <QWidget>
#include <QGridLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QDebug>
class CustWidget : public QWidget
{
    Q_OBJECT
public:
    CustWidget(QWidget *parent = nullptr):QWidget(parent)
    {
        QGridLayout *layout = new QGridLayout(this);

        // 创建按钮并添加到布局
        QPushButton *button0 = new QPushButton("0");
        button0->setObjectName("button0");
        layout->addWidget(button0, 5, 0);

        connect(button0,&QPushButton::clicked,this,[=](){
//             qDebug()<< sender()->objectName();
            //QObject -> QPushButton  下行转换
            QString text = ((QPushButton*)(sender()))->text();
            line->setText(text);
        });

        QPushButton *buttonMC = new QPushButton("MC");
        layout->addWidget(buttonMC, 0, 0);

        QPushButton *buttonMR = new QPushButton("MR");
        layout->addWidget(buttonMR, 0, 1);

        QPushButton *buttonMPlus = new QPushButton("M+");
        layout->addWidget(buttonMPlus, 0, 2);

        QPushButton *buttonMMinus = new QPushButton("M-");
        layout->addWidget(buttonMMinus, 0, 3);

        QPushButton *buttonMS = new QPushButton("MS");
        layout->addWidget(buttonMS, 0, 4);

        QPushButton *buttonM = new QPushButton("M");
        layout->addWidget(buttonM, 0, 5);

        QPushButton *buttonPercent = new QPushButton("%");
        layout->addWidget(buttonPercent, 1, 0);

        QPushButton *buttonCE = new QPushButton("CE");
        layout->addWidget(buttonCE, 1, 1);

        QPushButton *buttonC = new QPushButton("C");
        layout->addWidget(buttonC, 1, 2);

        QPushButton *buttonBackspace = new QPushButton("1/x");
        layout->addWidget(buttonBackspace, 1, 3);

        QPushButton *buttonSqrt = new QPushButton("x²");
        layout->addWidget(buttonSqrt, 1, 4);

        QPushButton *buttonNegativePositive = new QPushButton("±");
        layout->addWidget(buttonNegativePositive, 1, 5);

        QPushButton *button7 = new QPushButton("7");
        layout->addWidget(button7, 2, 0);
        connect(button7,&QPushButton::clicked,this,[=](){
            //static_cast
            //dynamic_cast
            //const_cast
            //reinterpret_cast
            line->insert(static_cast<QPushButton*>(sender())->text());

            //split  分割字符串
        });
        QPushButton *button8 = new QPushButton("8");
        layout->addWidget(button8, 2, 1);

        QPushButton *button9 = new QPushButton("9");
        layout->addWidget(button9, 2, 2);

        QPushButton *buttonDivide = new QPushButton("÷");
        layout->addWidget(buttonDivide, 2, 3);

        QPushButton *buttonSquareRoot = new QPushButton("√");
        layout->addWidget(buttonSquareRoot, 2, 4);

        QPushButton *buttonEquals = new QPushButton("=");
        layout->addWidget(buttonEquals, 2, 5, 3, 1);

        QPushButton *button4 = new QPushButton("4");
        layout->addWidget(button4, 3, 0);

        QPushButton *button5 = new QPushButton("5");
        layout->addWidget(button5, 3, 1);

        QPushButton *button6 = new QPushButton("6");
        layout->addWidget(button6, 3, 2);

        QPushButton *buttonMultiply = new QPushButton("×");
        layout->addWidget(buttonMultiply, 3, 3);

        QPushButton *buttonPower = new QPushButton("x^y");
        layout->addWidget(buttonPower, 3, 4);

        QPushButton *button1 = new QPushButton("1");
        layout->addWidget(button1, 4, 0);

        QPushButton *button2 = new QPushButton("2");
        layout->addWidget(button2, 4, 1);

        QPushButton *button3 = new QPushButton("3");
        layout->addWidget(button3, 4, 2);

        QPushButton *buttonSubtract = new QPushButton("-");
        layout->addWidget(buttonSubtract, 4, 3);

        QPushButton *buttonDecimal = new QPushButton(".");
        layout->addWidget(buttonDecimal, 4, 4);

        QPushButton *buttonPlus = new QPushButton("+");
        layout->addWidget(buttonPlus, 5, 3);

        line = new QLineEdit;
        layout->addWidget(line,5,1,1,2);
    }
    QLineEdit *line;
public slots:

};

class Calculator : public QMainWindow
{
    Q_OBJECT
public:
    explicit Calculator(QWidget *parent = nullptr);
    void Calculatorinit();
signals:

public slots:
};

#endif // CALCULATOR_H
